package com.tencent.sr.iris.activity.dependency.basic;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.tencent.sr.iris.activity.common.util.HttpUtil;
import com.tencent.sr.iris.basic.service.interfaces.client.IrisCommonKvConfigClient;
import com.tencent.sr.iris.basic.service.interfaces.constant.CommonKVBusinessTypeEnum;
import com.tencent.sr.iris.basic.service.interfaces.dto.TCommonKvConfigDTO;
import com.tencent.sr.iris.basic.service.interfaces.request.*;
import com.tencent.sr.iris.basic.service.interfaces.response.CommonConfigCountResponse;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import com.tencent.sr.rmall.common.request.PaginationRequest;
import com.tencent.sr.rmall.common.response.PaginationResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Component
@Slf4j
public class BasicDependency {

    @Resource
    private IrisCommonKvConfigClient irisCommonKvConfigClient;

    /**
     * 查询kv配置
     *
     * @return
     */
    public List<TCommonKvConfigDTO> batchQueryCommonKV(TCommonKvConfigBatchQueryRequest request) {
        request.setBusinessType(CommonKVBusinessTypeEnum.ACTIVITY.getCode());
        log.info("调用BasicDependency.batchQueryCommonKV，入参：{}", JSON.toJSONString(request));
        HttpResult<List<TCommonKvConfigDTO>> result = irisCommonKvConfigClient.batchQueryCommonKV(request);
        log.info("调用BasicDependency.batchQueryCommonKV结束，入参：{}，响应：{}", JSON.toJSONString(request),
                JSON.toJSONString(request));
        return HttpUtil.handleResult(result);
    }

    /**
     * 查询kv配置
     *
     * @return
     */
    public List<TCommonKvConfigDTO> batchQueryCommonKVNoCache(TCommonKvConfigBatchQueryRequest request) {
        request.setBusinessType(CommonKVBusinessTypeEnum.ACTIVITY.getCode());
        log.info("调用BasicDependency.batchQueryCommonKVNoCache，入参：{}", JSON.toJSONString(request));
        HttpResult<List<TCommonKvConfigDTO>> result = irisCommonKvConfigClient.batchQueryCommonKVNoCache(request);
        log.info("调用BasicDependency.batchQueryCommonKVNoCache结束，入参：{}，响应：{}", JSON.toJSONString(request),
                JSON.toJSONString(request));
        return HttpUtil.handleResult(result);
    }

    /**
     * 查询kv配置
     *
     * @return
     */
    public PaginationResponse<TCommonKvConfigDTO> queryCommonKVPage(
            PaginationRequest<TCommonKvConfigQueryPageRequest> request) {
        TCommonKvConfigQueryPageRequest queryCriteria = request.getQueryCriteria();
        queryCriteria.setBusinessType(CommonKVBusinessTypeEnum.ACTIVITY.getCode());
        log.info("调用BasicDependency.queryCommonKVPage，入参：{}", JSON.toJSONString(request));
        HttpResult<PaginationResponse<TCommonKvConfigDTO>> result = irisCommonKvConfigClient.queryCommonKVPage(request);
        log.info("调用BasicDependency.queryCommonKVPage，入参：{}，响应：{}", JSON.toJSONString(request),
                JSON.toJSONString(request));
        return HttpUtil.handleResult(result);
    }

    /**
     * 统计父级key子配置数
     *
     * @return
     */
    public List<CommonConfigCountResponse> countConfigUnderParentConfig(List<String> parentKeys) {
        if (CollectionUtil.isEmpty(parentKeys)) {
            return Collections.emptyList();
        }

        TCommonKvConfigCountRequest request = new TCommonKvConfigCountRequest();
        request.setBusinessType(CommonKVBusinessTypeEnum.ACTIVITY.getCode());
        request.setParentKeys(parentKeys);
        log.info("调用BasicDependency.countConfigUnderParentConfig，入参：{}", JSON.toJSONString(request));
        HttpResult<List<CommonConfigCountResponse>> result = irisCommonKvConfigClient
                .countConfigUnderParentConfig(request);
        log.info("调用BasicDependency.countConfigUnderParentConfig，入参：{}，响应：{}", JSON.toJSONString(request),
                JSON.toJSONString(request));
        return HttpUtil.handleResult(result);
    }

    /**
     * 新增
     *
     * @return
     */
    public Boolean add(TCommonKvConfigAddRequest request) {
        request.setBusinessType(CommonKVBusinessTypeEnum.ACTIVITY.getCode());
        log.info("调用BasicDependency.add，入参：{}", JSON.toJSONString(request));
        HttpResult<Boolean> result = irisCommonKvConfigClient.add(request);
        log.info("调用BasicDependency.add，入参：{}，响应：{}", JSON.toJSONString(request), JSON.toJSONString(request));
        return HttpUtil.handleResult(result);
    }

    /**
     * 更新
     *
     * @return
     */
    public Boolean update(TCommonKvConfigUpdateRequest request) {
        request.setBusinessType(CommonKVBusinessTypeEnum.ACTIVITY.getCode());
        request.setParentKey(null);
        log.info("调用BasicDependency.update，入参：{}", JSON.toJSONString(request));
        HttpResult<Boolean> result = irisCommonKvConfigClient.update(request);
        log.info("调用BasicDependency.update，入参：{}，响应：{}", JSON.toJSONString(request), JSON.toJSONString(request));
        return HttpUtil.handleResult(result);
    }
}
